package com.sakana.studentsystem;

import java.awt.geom.FlatteningPathIterator;
import java.util.*;
import java.util.function.Predicate;

public class APP {

    public static void main(String[] args) {
        //创建一个用户集合
        ArrayList<User> list = new ArrayList<>();

        //创建用户需要的操作
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("-----------登录界面------------");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("请输入你需要的操作：");

            //录入需要的操作
            String choose = sc.next();
            switch (choose) {
                case "1" -> {
                    logIn(list);
                    break loop;
                }
                case "2" -> {
                    register(list);
                }
                case "3" -> {
                    forget(list);
                }
                default -> System.out.println("输入错误，请重新输入");
            }
        }

    }

    //登录
    public static void logIn(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            //1.键盘输入用户名
            System.out.println("输入用户名：");
            String name = sc.next();
            //2.输入密码
            System.out.println("输入密码：");
            String password = sc.next();
            //3.生成一个验证码
            String code = getVerificationCode();
            System.out.println("验证码：" + code);
            //4.输入验证码
            System.out.println("输入验证码：");
            String inputCode = sc.next();


            //如果用户名不存在，提示未注册
            boolean flag = contains(list, name);
            if (!flag) {
                System.out.println("该用户未注册,还有" + (3 - i) + "次机会");
                continue;
            }
            //存在的判断密码和验证码是否正确
            int index = getIndex(list, name);
            if (!list.get(index).getPassword().equals(password)) {
                System.out.println("密码错误,还有" + (3 - i) + "次机会");
                continue;
            }
            if (!inputCode.equalsIgnoreCase(code)) {
                System.out.println("验证码错误,还有" + (3 - i) + "次机会");
                continue;
            }

            System.out.println("登录成功");
            StudentSystem ss=new StudentSystem();
            ss.startStudentSystem();
            break;
        }

    }

    //注册
    public static void register(ArrayList<User> list) {
        //创建一个user类
        User u = new User();

        //获得正确的用户名
        String currentName = getCurrentName(list);
        if (!currentName.isEmpty()) {
            u.setUserName(currentName);
        } else {
            currentName = getCurrentName(list);
        }

        //输入密码
        String password = getPassword();
        u.setPassword(password);

        //输入身份证号
        String personId = getPersonId();
        if (!personId.isEmpty()) {
            u.setPersonId(personId);
        }

        //输入手机号码
        String phoneNum = getPhoneNum();
        if (!phoneNum.isEmpty()) {
            u.setPhoneNum(phoneNum);
        }

        list.add(u);
        System.out.println("注册成功");
    }

    //忘记密码
    public static void forget(ArrayList<User> list) {
        //键盘录入用户名
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = sc.next();
        String personId;
        String phoneNum;
        //判断是否存在
        if (contains(list, name)) {
            //键盘录入身份证号和手机号
            System.out.println("请输入身份证号：");
            personId = sc.next();
            System.out.println("请输入手机号");
            phoneNum = sc.next();
            //判断身份证和手机号是否相同
            int index = getIndex(list, name);
            if (list.get(index).getPersonId().equalsIgnoreCase(personId) && list.get(index).getPhoneNum().equals(phoneNum)) {
                //输入要修改的新的密码
                String password = getPassword();
                if (!password.isEmpty()) {
                    list.get(index).setPassword(password);
                    System.out.println("修改成功");
                }
            } else {
                System.out.println("输入错误，修改失败");
            }


        } else {
            System.out.println("用户不存在");
            return;
        }
    }

    //获取name的下标
    public static int getIndex(ArrayList<User> list, String name) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUserName().equals(name)) {
                return i;
            }
        }
        return -1;
    }

    //判断name是否存在
    public static boolean contains(ArrayList<User> list, String name) {
        return getIndex(list, name) >= 0;
    }

    //规范名字
    public static String getCurrentName(ArrayList<User> list) {
        //注册用户名
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = sc.next();
        //是否存在
        boolean flag = contains(list, name);
        if (flag) {
            System.out.println("已存在该用户名");
            return "";
        }
        //长度是否在3-15间
        int length = name.length();
        if (length < 3 || length > 15) {
            System.out.println("长度在3-15间");
            return "";
        }
        //字母加数字的组合，不能纯数字
        char[] nameCharArray = name.toCharArray();
        int count = 0;
        for (int i = 0; i < nameCharArray.length; i++) {
            char c = nameCharArray[i];
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                    count++;
                }
            } else {
                System.out.println("输入非法字符");
                return "";
            }
        }
        if (count == 0) {
            System.out.println("只能是字母加数字的组合");
            return "";
        }
        return name;
    }

    //输入密码
    public static String getPassword() {
        //键盘录入密码
        String password1;
        String password2;
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入第一次密码：");
            password1 = sc.next();
            System.out.println("请输入第二次密码");
            password2 = sc.next();
            if (password2.equals(password1)) {
                break;
            } else {
                System.out.println("两次密码不同，请重新输入密码");
            }
        }
        return password1;
    }

    //输入身份证号
    public static String getPersonId() {
        //键盘录入身份证号
        String personid;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入身份证号：");
            personid = sc.next();
            //判断长度是否为18
            if (personid.length() != 18) {
                System.out.println("长度必须为18");
                continue;
            }
            //判断第一位是否为0
            char c = personid.charAt(0);
            if (c == '0') {
                System.out.println("第一位不可为0");
                continue;
            }
            //前17为必须为数字
            for (int i = 0; i < personid.length() - 1; i++) {
                char c1 = personid.charAt(i);
                if (c1 > '9' || c1 < '0') {
                    System.out.println("前17位数必须为数字");
                    continue;
                }
            }

            //最后一位是数字也可以大小写x
            char c2 = personid.charAt(17);
            if ((c2 >= '0' && c2 <= '9') || (c2 == 'x' || c2 == 'X')) {
                return personid;
            } else {
                System.out.println("最后一位不符合规范");
            }
        }

    }

    //输入手机号码
    public static String getPhoneNum() {
        //键盘输入号码
        Scanner sc = new Scanner(System.in);
        String num = null;
        while (true) {
            System.out.println("请输入电话号码：");
            num = sc.next();
            //长度为11
            if (num.length() != 11) {
                System.out.println("长度为11");
                return "";

            }
            //不能以0为开头
            char c = num.charAt(0);
            if (c == '0') {
                System.out.println("不能以0开头");
                return "";
            }
            //必须都为数字
            for (int i = 0; i < num.length(); i++) {
                char c1 = num.charAt(i);
                if (c1 < '0' || c1 > '9') {
                    System.out.println("必须都为数字");
                    return "";
                }
            }
            break;
        }

        return num;
    }

    //生成一个验证码
    public static String getVerificationCode() {
        //1.将52个英文字母放入数组
        char[] arr = new char[52];
        char c1 = 'A';
        char c2 = 'a';
        for (int i = 0; i < arr.length; i++) {
            if (i < 26) {
                arr[i] = c1++;
            } else {
                arr[i] = c2++;
            }
            //System.out.println(arr[i]);
        }
        //2.随机取4位字母放入字符串
        String res = "";
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int Index = r.nextInt(arr.length);
            res = res + arr[Index];
        }
        //System.out.println(res);

        //3.随机一位数字放入最后一位
        res = res + r.nextInt(9);

        //4.将数字随机放入一位
        char[] charArray = res.toCharArray();
        //交换
        int num = r.nextInt(res.length() - 1);
        char temp = charArray[charArray.length - 1];
        charArray[charArray.length - 1] = charArray[num];
        charArray[num] = temp;

        res = new String(charArray);

        return res;
    }

}
